package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.EsignCompanyBean;
import com.molichuxing.services.infrastructure.bean.EsignDealerBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EsignDealerMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EsignDealerCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignDealerModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EsignDealerDto;
import com.molichuxing.services.infrastructure.service.EsignDealerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 经销商印章 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-10-08
 */
@Service("esignDealerService")
public class EsignDealerServiceImpl implements EsignDealerService {

    @Autowired
    EsignDealerMapper esignDealerMapper;

    /**
     * 添加
     *
     * @param esignDealerCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer createEsignDealer(EsignDealerCreateDto esignDealerCreateDto) throws Exception {
        if (null == esignDealerCreateDto) {
            throw new BizException("经销商印章生成失败");
        }

        EsignDealerBean esignDealerBean = Convert.toEsignDealerBean(esignDealerCreateDto);
        esignDealerMapper.create(esignDealerBean);
        return esignDealerBean.getId();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public EsignDealerDto getEsignDealerById(Integer id) {
        EsignDealerBean esignDealerBean = esignDealerMapper.getById(id);
        if (null == esignDealerBean) {
            return null;
        }
        return Convert.toEsignDealerDto(esignDealerBean);
    }

    /**
     * 修改
     *
     * @param esignDealerModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyEsignDealer(EsignDealerModifyDto esignDealerModifyDto) throws Exception {
        if (null == esignDealerModifyDto) {
            throw new BizException("经销商印章生成失败");
        }
        Integer id = esignDealerModifyDto.getId();
        if (null == id) {
            throw new BizException("经销商印章生成失败");
        }
        EsignDealerBean esignDealerBean = esignDealerMapper.getById(id);
        if (null == esignDealerBean) {
            throw new BizException("经销商印章生成失败");
        }

        Convert.toEsignDealerBean(esignDealerModifyDto, esignDealerBean);
        return (esignDealerMapper.modifyById(esignDealerBean) > 0) ? true : false;
    }

    /**
     * 获取集合
     *
     * @return
     */
    @Override
    public Map<Integer, EsignDealerDto> getEsignDealerMap() {
        List<EsignDealerBean> allList = esignDealerMapper.getAllList();
        if (null == allList || allList.size() < 1) {
            return null;
        }

        List<EsignDealerDto> esignDealerDtoList = Convert.toEsignDealerDtoList(allList);
        return esignDealerDtoList.stream().collect(Collectors.toMap(EsignDealerDto::getEsignDealerId, a -> a, (k1, k2) -> k1));
    }


}
